package org.gurms.web.action;

import org.gurms.common.util.JqueryUtil;
import org.gurms.common.util.ReflectionUtil;
import org.gurms.service.dto.Page;

/**
 * Struts2中典型CRUD Action的抽象基类.
 * 
 * 主要定义了对ModelDriven接口的使用,以及CRUD函数和返回值的命名.
 *
 * @param <T> CRUDAction所管理的对象类型.
 * 
 */
@SuppressWarnings("serial")
public abstract class CRUDActionSupport<T> extends BaseAction{

	//每页显示数据条数
	protected int pagesize = 10;
	//分页参数
	protected Page<T> page = new Page<T>(pagesize);
	//对象ID
	protected String objId;
	
	protected Class clz;
	
	//页面效验脚本
	protected String script;
	
	public CRUDActionSupport(){
		super();
		clz = ReflectionUtil.getSuperClassGenricType(getClass());
	}
	
	public String getObjId() {
		return objId;
	}

	public void setObjId(String objId) {
		this.objId = objId;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	
	public Page<T> getPage() {
		return page;
	}

	public String getScript() {
		return script;
	}

	public void setScript(String script){
		this.script = script;
	}
	
	/**
	 * 返回flexigrid的json对象
	 * @param data 数据集
	 * @param idField T对应的ID字段名
	 * @return
	 */
	protected String renderFlexigrid(Page<T> data,String idField){
		Struts2ActionUtil.renderJson(JqueryUtil.createFlexigridJson(data, clz, idField));
		return NONE;
	}
	
	/**
	 * 生成页面验证脚本
	 * @throws Exception
	 */
	protected void createValidScript() throws Exception{
		Class clz = this.clz;
		StringBuffer valid = new StringBuffer(200);
		this.script = valid.toString();
	}
	
	/**
	 * 执行服务端验证
	 * @return
	 */
	protected boolean executeServiceValid(){
		return true;
	}
	
	// CRUD函数 //
	/**
	 * Action函数,默认action函数，显示Entity列表.
	 */
	@Override
	public abstract String execute() throws Exception;

	/**
	 * Action函数,新增或修改Entity. 
	 * 建议return RELOAD.
	 */
	public abstract String save() throws Exception;

	/**
	 * Action函数,删除Entity.
	 * 建议return RELOAD.
	 */
	public abstract String delete() throws Exception;

	/**
	 * Action函数,显示单个Entity.
	 * 建议return DETAIL.
	 */
	public abstract String detail() throws Exception;

}
